import {UA} from '../utils/head.js'
let md5 = require('../utils/md5.js')
let ua=new UA()

class RequestModel{
  getsystemTime({urlType,url}){
    return new Promise((resolve,reject)=>{
      wx.request({
        url: this._getRequestUrl(urlType)+url,
        success:res=>{
          resolve(res.data.data)
        }
      })
    })
  }
  getWxcode(){
    return new Promise((resolve, reject) => {
      wx.login({
        success:res=>{
          resolve(res.code)
        }
      })
    })
  }
  requestGet({urlType,url}){
    return new Promise((resolve, reject) => {
      wx.request({
        url: this._getRequestUrl(urlType) + url,
        success: res => {
          resolve(res.data)
        }
      })
    })
  }
  requestPost({urlType, url, reqData, islogin}){
    let app = getApp()
    //添加固定参数
    reqData['channel'] = 'Mobile';
    reqData['clientId'] = 'haiwaigou';
    reqData['terminalType'] = 'IOS';
    reqData['version'] = '300';
    //添加时间戳
    let timestamp = (new Date()).valueOf()
    timestamp = parseInt(timestamp / 1000);
    timestamp -= app.systemTimeGap;
    reqData['t'] = timestamp
    //如果已登录,需要添加accessToken
    if (islogin) {
      if (app.accessTokenDic.refreshToken == '') {
        console.log(`100102://必须先登录`);
        return;
      }
      let accessToken = app.accessTokenDic.accessToken;
      reqData['accessToken'] = accessToken;
    }
    //添加md5验证，参数排序
    let sdic = Object.keys(reqData).sort();
    let pStr = ''
    for (var ki in sdic) {
      var dstr = reqData[sdic[ki]];
      pStr += sdic[ki] + '=' + dstr;
      if (ki < sdic.length - 1) {
        pStr += '&';
      }
    }//如果已经登陆 需要添加authorizationCode 混淆
    if (islogin) {
      var authorizationCode = app.accessTokenDic.authorizationCode;
      //console.log(authorizationCode)
      if (authorizationCode.length > 0) {
        pStr += '&appKey=' + authorizationCode;
      }
    }
    let h = md5.hexMD5(pStr)
    reqData['h'] = h
    return new Promise((resolve, reject) => {
      wx.request({
        url: this._getRequestUrl(urlType) + url,
        data:reqData,
        method:'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          'hwg-useragent': ua.getUseragent()
        },
        success: res => {
          resolve(res.data)
        }
      })
    })
  }
  requestLocation(){
    return new Promise((resolve,reject)=>{
      wx.getLocation({
        success:res=>{
          resolve(res)
        },
        fail:err=>{
          reject(err)
        }
      })
    })
  }
  getScreenBrightness(){
    return new Promise((resolve, reject) => {
      wx.getScreenBrightness({
        success:res=>{
          resolve(res.value)
        }
      })
    })
  }
  setScreenBrightness(light) {
    return new Promise((resolve, reject) => {
      wx.setScreenBrightness({
        value: light,
        success:res=>{
          resolve(res)
        }
      })
    })
  }
  requestYZM({urlType,url,reqData,islogin}){
    return new Promise((resolve, reject) => {
      wx.request({
        url: this._getRequestUrl(urlType) + url,
        data: reqData,
        method: 'POST',
        success:res=>{
          resolve(res)
        }
      })
    })
  }
  requestScore({urlType,url}){
    return new Promise((resolve, reject) => {
      wx.request({
        url: this._getRequestUrl(urlType) + url,
        method: 'POST',
        success: res => {
          resolve(res.data.score)
        }
      })
    })
  }



  //所有接口收集
  _getRequestUrl(urlType) {
    let url = '';
    if (urlType == 'ORDERTK') {
      return 'https://order.tepin.hk/';
    } else if (urlType == 'PRODUCTTK') {
      return 'https://product.tepin.hk/';
    } else if (urlType == 'MARKETINGTK') {
      return 'https://marketing.tepin.hk/';
    } else if (urlType == 'SNSTC') {
      return 'https://sns.tepin.com/';
    } else if (urlType == 'MEMBERTK') {
      return 'https://member.tepin.hk/';
    } else if (urlType == 'ATK') {
      return 'https://a.tepin.hk/yx/index.php/api/';
    } else if (urlType == 'ATKPay') {
      return 'https://a.tepin.hk/yx/';
    } else if (urlType == 'SEARCHHK') {
      return 'https://search.tepin.hk/';
    } else if (urlType == 'LOGINCOM') {
      return 'https://login.tepin.com/';
    } else if (urlType == 'HWGCMS') {
      return 'https://hwgcmsadmin.tepin.hk/api/';
    }
    return url;
  }
}

export { RequestModel}










